#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2025/4/24 19:54
# @Author   : MingTai
# @File     : video_clip_extraction.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2025/4/24 19:54
# @Author   : MingTai
# @File     : video_clip_extraction.py
from moviepy.editor import VideoFileClip, concatenate_videoclips
import math

from app.utils.time_util.time_handle import get_second_time_str
from app.utils.video_utile import video_path_url, video_path_url2


def extract_average_clips(input_video_path, output_video_path, num_clips=5, clip_duration=5):
    # 加载视频文件
    video = VideoFileClip(input_video_path)
    video_duration = video.duration

    # 计算每个片段的起始时间
    clip_start_times = []
    interval = (video_duration - clip_duration) / (num_clips - 1)
    for i in range(num_clips):
        start_time = i * interval
        clip_start_times.append(start_time)

    # 截取片段
    clips = []
    for start_time in clip_start_times:
        end_time = start_time + clip_duration
        clip = video.subclip(start_time, end_time)
        clips.append(clip)

    # 拼接片段
    final_clip = concatenate_videoclips(clips)

    # 保存拼接后的视频
    final_clip.write_videofile(output_video_path)

    # 关闭视频文件
    video.close()
    for clip in clips:
        clip.close()
    final_clip.close()


# 平均切割视频一段70秒
if __name__ == "__main__":
    # 修改，替换为实际的输入视频文件路径
    input_video = video_path_url2

    output_video = f'video_color_{get_second_time_str()}.mp4'  # 替换为实际的输出视频文件路径
    # 时长修改这两个参数
    num_clips = 23  # 要截取的片段数量
    clip_duration = 3  # 每个片段的时长（秒）
    extract_average_clips(input_video, output_video, num_clips, clip_duration)
